<?php
/**
 * 问题处理进度
 */
class Application_Model_ProblemWay extends Application_Model_DomainObject {

    protected $_id;
    protected $_wayId;
    protected $_status;
    protected $_schedule;
    protected $_type;
    protected $_isMessage;
    protected $_dateMessage;
    /**
     * 待客户回复
     */
    const Problem_StayUser = 1;
    /**
     * 客户已回复
     */
    const Problem_StayUuch = 2;
    /**
     * 部分退回
     */
    const Problem_PartReturn = 11;
    /**
     * 全部退回
     */
    const Problem_ReturnCancel = 12;
   
    /**
     * 取消
     */
    const Problem_Cancel = -1;
    /**
     * 放行
     */
    const Problem_Allclear = 100;
    /**
     * 初始化
     */
    const Schedule_Default = 0;
    /**
     * 申请退回
     */
    const Schedule_ApplyFor = 1;
    /**
     * 退回途中
     */
    const Schedule_ReturnRoute = 2;
    /**
     * 已退回渠道商
     */
    const Schedule_InChannel = 3;
    /**
     * 已退回悠奇
     */
    const Schedule_InUuch = 4;
    /**
     * 已入库
     */
    const Schedule_InStore = 5;
    /**
     * 申请退包类型
     */
    const Schedule_Reason = 200;
    /**
     * 国际运单
     */
    const Type_Bill = 1;
    /**
     * 上门取件
     */
    const Type_Visiting =2;
     /**
     * 留言待用户回复
     */
    const IsMessage_User = 21;
    /**
     * 用户已回复
     */
    const IsMessage_Uuch = 22;
    public function getStatusName()
    {
        switch ($this->_status) {
            case Application_Model_ProblemWay::Problem_StayUser:
                return "等待客户回复";
                break;
            case Application_Model_ProblemWay::Problem_StayUuch:
                return "客户已回复";
                break;
            case Application_Model_ProblemWay::Problem_PartReturn:
                return "部分退回";
                break;
            case Application_Model_ProblemWay::Problem_ReturnCancel:
                return "退件";
                break;
            case Application_Model_ProblemWay::Problem_Allclear:
                return "放行";
                break;
            default:
                return "未知状态";
                break;
        }
    }


    public static function getProblemScheduleByCount($problem = null, $schedule = null ,$type = Application_Model_ProblemWay::Type_Bill) {
        $problemwaySearch = new Application_Model_Filter_ProblemWaySearch();
        $where = $problemwaySearch->getSearchFileds();
        $result = null;
        if($where != array()){
            $result = Application_Model_wayBillItem::getDao()->queryAll($where, " datePaid desc ");
        }
        $waywhere = array();
        $wayand = "";
        if ($result != null) {
            $wayId = "(";
            foreach ($result as $wayVal) {
                $wayId .= $wayVal->id . ",";
            }
            $wayId .= "0)";
            $waywhere[" wayId in "] = $wayId;
            $wayand = " and ";
        }
        if ($problem != null) {
            
            $waywhere[$wayand . " status = "] = $problem;
            $wayand = " and ";
        }
        if ($schedule != null) {
            $waywhere[$wayand . " schedule = "] = $schedule;
            $wayand = " and ";
        }else{
            $waywhere[$wayand . " schedule is null and "] = 1;
            $wayand = " and ";
        }
        $waywhere[$wayand . " type =  "] = $type;

        return self::getDao()->count($waywhere);
    }

    public static function getBillAll($problemwaySearch,$pagesize, $pageindex,$stus,$schedule,$isMessage)
   {
        $where = $problemwaySearch->getSearchFileds();
        $result = null;
        if($where != array()){
            $result = Application_Model_wayBillItem::getDao()->queryAll($where, " datePaid desc ");
        }
        $waywhere = array();
        $wayand = "";
        $order = " wayId desc ";
        if ($result != null) {
            $wayId = "(";
            foreach ($result as $wayVal) {
                $wayId .= $wayVal->id . ",";
            }
            $wayId .= "0)";
            $waywhere[" wayId in "] = $wayId;
            $wayand = " and ";
        }
        if ($stus != null)
        {
            $waywhere[$wayand." status = "] = $stus;
            $wayand = " and ";
        }
        if ($schedule != null)
        {
            $waywhere[$wayand." schedule = "] = $schedule;
            $wayand = " and ";
        }else{
            $where[$wayand . " schedule is null and "] = 1;
            $and = " and ";
        }
        if ($isMessage != null){
            $waywhere[$wayand." isMessage = "] = $isMessage;
            $wayand = " and ";
            $order = " dateMessage desc";
        }
//        var_dump($waywhere);
        return self::getDao()->queryPage($waywhere,$order, $pagesize, $pageindex);
    }

    public static function getByTypeWayId($wayId,$type = Application_Model_ProblemWay::Type_Bill) {
        return self::getDao()->queryOne(array('wayId = '=>$wayId," and type = "=>$type));
    }

    public static function addProblemWay($billid,$status, $schedule) {
        $problemItem = self::getWayId($billid);
        if (!$problemItem) {
            $problemItem = new Application_Model_ProblemWay();
        }
        $problemItem->wayId = $billid;
        $problemItem->status = $status;
        $problemItem->schedule = $schedule;
        $problemItem->save();
    }
    
    private $_waybill;
    public function getWay()
    {
        if(!$this->_waybill){
            $this->_waybill = Application_Model_wayBillItem::load($this->_wayId);
        }
        return $this->_waybill;
    }
    private $_scheduleDetaile;
    public function getReasonBySchedule()
    {
        if(!$this->_scheduleDetaile){
            $where[" wayId = "] = $this->_wayId;
            $where[" and schedule = "] = Application_Model_ProblemWay::Schedule_Reason;
            $this->_scheduleDetaile = Application_Model_ProblemSchedule::getDao()->queryOne($where);
        }
        return $this->_scheduleDetaile;
        
    }
}